const  http=require("http");
const  server=http.createServer();

server.on("request",(req,res)=>{
    if(req.url==="/"){
        const  cookie={};
        req.headers.cookie &&  req.headers.cookie.split("; ").forEach(item=>{
            const  parts=item.split("=");
            cookie[parts[0]]=parts[1];
        });

        if(cookie.isvisit==="yes"){
            res.writeHeader(200,{"Content-Type":"text/html;charset=utf8"});
            res.end("这是再次来的")
        }else{
            const  expiresTime=new Date(Date.now()+10*1000).toUTCString();  //转换成世界时间
            res.writeHeader(200,{
                "Content-Type":"text/html;charset=utf8",
                // "Set-Cookie":["isvisit=yes","test=okk"]
                "Set-Cookie":["isvisit=yes; expires="+expiresTime, "test=okk"]
            });
            res.end("这是初次来的吧");
        }
    }else{
        res.end("404");
    }
});

server.listen(4321,()=>{
    console.log('server running at http://127.0.0.1:4321')
})